﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace SEO.Spider
{
    /// <summary>
    /// 版权所有: 版权所有(C) 2011，Adin Lee
    /// 内容摘要: 域名Whois查询接口
    /// 完成日期：2011年6月4日
    /// 版    本：V1.0 
    /// 作    者：Adin
    /// </summary>
    public class DomainWhois
    {
        /// <summary>
        /// 根据网址获取域名Whois信息
        /// </summary>
        /// <param name="strWebUrl">网址</param>
        /// <returns>返回域名Whois信息</returns>
        public static string GetWhoisRegTime(string strWebUrl)
        {
            string strReturnValue = "-";

            try
            {
                //清除www.
                if (strWebUrl.Contains("www"))
                {
                    strWebUrl = strWebUrl.Replace("www.", "");
                }

                strWebUrl = "http://whoisv1.hichina.com/cgi-bin/whois?domain=" + strWebUrl;
                SEO.Utility.WebClient wc = new SEO.Utility.WebClient();
                wc.Encoding = System.Text.Encoding.UTF8;
                wc.OpenRead(strWebUrl);
                string strGetHtmlCode = wc.RespHtml;

                Regex rx = new Regex("Creation Date:(?<content>[\\s\\S]*)?Expiration Date:", RegexOptions.Singleline);
                string strGetResult = rx.Match(strGetHtmlCode).Result("${content}");
                strGetResult = SEO.Utility.StringHelper.CleanHtml(strGetResult);
                strGetResult = strGetResult.Replace("\r", "");
                strGetResult = strGetResult.Replace("\n", "");
                strGetResult = strGetResult.Replace("\t", "");

                strReturnValue = strGetResult;
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("根据网址获取域名Whois信息 -> " + ex.Message);
            }

            return strReturnValue;
        }
    }
}
